(ns bowling)

(def score)

(defn- nil-to-0 [value]
  (if (nil? value) 0 value))

(defn- first-or-0 [sequence]
  (nil-to-0 (first sequence)))

(defn- second-or-0 [sequence]
  (nil-to-0 (second sequence)))

(defn- spare? [ball-1 ball-2]
  (= 10 (+ ball-1 ball-2)))

(defn- strike? [ball]
  (= 10 ball))

(defn- strike[rolls frame-no]
  (+ 	
    10 
    (first-or-0 rolls)
    (second-or-0 rolls)		
    (score rolls frame-no)))

(defn- non-strike [ball-1 ball-2 rolls frame-no]
  (+ 	
    ball-1
    ball-2
    (if (spare? ball-1 ball-2)
      (first-or-0 rolls)
      0)			
    (score rolls frame-no)))

(defn score 
  ([rolls] (score rolls 0))
  ([rolls frame-no]
    (let [frame-no (inc frame-no) 
          ball-1 (first-or-0 rolls) 
          ball-2 (second-or-0 rolls)]
      (if (> frame-no 10)			
        0
        (if (strike? ball-1)
          (strike (rest rolls) frame-no)
          (non-strike ball-1 ball-2 (drop 2 rolls) frame-no))))))